import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 最小栈的实现
 * O(1)时间内取出最小值
 * User: xwl
 */
public class MinStack {
    // 定义一个栈
    private Stack<Integer> stack;
    // 定义一个栈来存储最小值
    private Stack<Integer> minStack;

    // 构造方法
    public MinStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    /**
     * 压栈
     * @param val:待压入的值
     */
    public void push(int val) {
        // 压栈
        stack.push(val);
        // 维护最小栈
        if (minStack.empty()) {
            minStack.push(val);
        }else {
            if (val <= minStack.peek() ) {
                minStack.push(val);
            }
        }
    }

    /**
     * 弹栈
     */
    public void pop() {
        // 判断
        if (!stack.empty()) {
            // 弹栈并用val来接收栈顶值
            int val = stack.pop();
            // 维护最小栈
            if (val == minStack.peek()) {
                minStack.pop();
            }
        }
    }

    /**
     * 查看栈顶值
     * @return stack.peek():栈顶值 -1:栈空
     */
    public int top() {
        // 判断
        if (!stack.empty()) {
            return stack.peek();
        }
        return -1;
    }

    /**
     * 获取最小值
     * @return minStack.pop():最小栈的栈顶值
     */
    public int getMin() {
        return minStack.peek();
    }
}